package com.huaiwei.usmManager.modules.usm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaiwei.usmManager.common.utils.Constant;
import com.huaiwei.usmManager.common.utils.NumberUtils;
import com.huaiwei.usmManager.common.utils.PageUtils;
import com.huaiwei.usmManager.common.utils.Query;
import com.huaiwei.usmManager.modules.usm.dao.CabinetDao;
import com.huaiwei.usmManager.modules.usm.dao.GoodsDao;
import com.huaiwei.usmManager.modules.usm.entity.*;
import com.huaiwei.usmManager.modules.usm.service.CabinetService;
import com.huaiwei.usmManager.modules.usm.service.GoodsService;
import com.huaiwei.usmManager.modules.usm.service.ResourceService;
import com.huaiwei.usmManager.modules.usm.service.ShopService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CabinetServiceImpl  extends ServiceImpl<CabinetDao, CabinetEntity> implements CabinetService {

    @Autowired
    private CabinetDao cabinetDao;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ShopService shopService;


    //分页
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String number=(String) params.get("number");
        Integer status= NumberUtils.obj2Integer(params.get("status"));
        IPage<CabinetEntity> page=this.page(new Query<CabinetEntity>().getPage(params),
                new QueryWrapper<CabinetEntity>()
                .like(StringUtils.isNotEmpty(number),"number",number)
                .eq(status!=null,"status",status)
                );
        return new PageUtils(page);
    }

    @Override
    public CabinetEntity getInfoByCabinetId(Integer cabinetId) {
        CabinetEntity cabinetEntity=this.getById(cabinetId);/*先拿到商品的信息*/
         /*存入图片的list*/
        /*查询电影的剧照列表*/
        List<ResourceEntity> imageList=getResourceListByCabinetId(cabinetId);
        cabinetEntity.setImageList(imageList);
        return cabinetEntity;
    }


    //根据展示柜id来查看当前是否有商品在展示柜上
    @Override
    public boolean findGoodsInCabByCabId(int cabinetId) {
       if(cabinetDao.findGoodsInCabByCabId(cabinetId)>0){
           return false;
       }else {
           return true;
       }
    }

    /**
     * 根据cabinetId查出他的包含的商品(分页)
     * @param cabinetId
     * @return
     */
    @Override
    public PageUtils getGoodsByCabinetId(Integer cabinetId, Map<String, Object> params) {
        IPage<GoodsEntity> page=goodsService.page(new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>().eq("cabinet_id",cabinetId));
        return new PageUtils(page);
    }


    /**
     * 根据cabinetId查出所属的商铺信息
     * @param cabinetId
     * @return
     */
    @Override
    public PageUtils getShopByCabinetId(Integer cabinetId, Map<String, Object> params) {

        int getShopId = cabinetDao.getShopIdByCabinetId(cabinetId);
        IPage<ShopEntity> page=shopService.page(new Query<ShopEntity>().getPage(params),
                new QueryWrapper<ShopEntity>().eq("shop_id",getShopId));
        return new PageUtils(page);
    }
    @Override
    public int getShopIdByCabinetId(Integer cabinetId) {
        return cabinetDao.getShopIdByCabinetId(cabinetId);
    }

    //查询展示柜的Resourcelist信息
    @Override
    public List<ResourceEntity> getResourceListByCabinetId(Integer cabinetId) {
        return resourceService.list(new QueryWrapper<ResourceEntity>()
                .eq("resource_id",cabinetId)
                .eq("type", Constant.CABINET)
                .eq("status",Constant.ON));
    }


    @Override
    public void insertOrUpdate(CabinetEntity cabinetEntity) {
        if(cabinetEntity.getCabinetId()!=null){
            deleteRelationInfo(cabinetEntity.getCabinetId());/*删除关联信息完之后往下运行，更新即是新增*/
        }
        if(this.saveOrUpdate(cabinetEntity)){
            /*插入图片信息*/
            if(cabinetEntity.getImageUrls()!=null&&cabinetEntity.getImageUrls().length>0){
                /*数组转List*/
                List<ResourceEntity> imageList=new ArrayList<>();
                for(String image:cabinetEntity.getImageUrls()){
                    ResourceEntity imageResource=new ResourceEntity();
                    imageResource.setResourceId(cabinetEntity.getCabinetId());
                    imageResource.setType(Constant.CABINET);
                    imageResource.setImageUrl(image);
                    imageResource.setStatus(1);
                    imageList.add(imageResource);
                }
                /*批量添加图片*/
                resourceService.saveBatch(imageList);
            }
        }
    }

    @Override
    public void deleteInfo(Integer cabinetId) {
        if(this.removeById(cabinetId)){
            deleteRelationInfo(cabinetId);
        }
    }

    //删除关联的信息
    public void deleteRelationInfo(Integer cabinetId){
        /*删除图片*/
        resourceService.remove(new QueryWrapper<ResourceEntity>()
                .eq("resource_id",cabinetId)
                .eq("type", Constant.CABINET)
                .eq("status",Constant.ON));
    }

}
